﻿using SAEA.MVC;
using KeydbCommand.Models;
using KeydbCommand.Attr;
using KeydbCommand.Services;

namespace KeydbCommand.Controllers
{
    /// <summary>
    /// keydb相关api
    /// </summary>
    [Auth(true)]
    public class KeydbController : Controller
    {
        /// <summary>
        /// 连接到keydb
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Connect(string name)
        {
            return Json(new KeydbService().Connect(name));
        }

        /// <summary>
        /// 获取服务器信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public ActionResult GetInfoString(string name)
        {
            return Json(new KeydbService().GetInfoString(name));
        }

        /// <summary>
        /// 获取客户端连接信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public ActionResult GetClients(string name)
        {
            return Json(new KeydbService().GetClients(name));
        }




        /// <summary>
        /// 获取db中的元素数量
        /// </summary>
        /// <param name="name"></param>
        /// <param name="dbIndex"></param>
        /// <returns></returns>
        public ActionResult GetDBSize(string name, int dbIndex)
        {
            return Json(new KeydbService().GetDBSize(name, dbIndex));
        }

        /// <summary>
        /// 获取keys
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="name"></param>
        /// <param name="dbIndex"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public ActionResult GetKeyTypes(int offset, string name, int dbIndex, string key)
        {
            return Json(new KeydbService().GetKeyTypes(offset, name, dbIndex, key));
        }

        /// <summary>
        /// 获取ttls
        /// </summary>
        /// <param name="name"></param>
        /// <param name="dbIndex"></param>
        /// <param name="keys"></param>
        /// <returns></returns>
        public ActionResult GetTtls(string name, int dbIndex, string keys)
        {
            return Json(new KeydbService().GetTtls(name, dbIndex, keys));
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="name"></param>
        /// <param name="dbIndex"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public ActionResult BatchRemove(string name, int dbIndex, string key)
        {
            return Json(new KeydbService().BatchRemove(name, dbIndex, key));
        }

        /// <summary>
        /// 提交或修改数据
        /// </summary>
        /// <param name="keydbData"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Set(KeydbData keydbData)
        {
            return Json(new KeydbService().Set(keydbData));
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="keydbData"></param>
        /// <returns></returns>
        public ActionResult GetCount(KeydbData keydbData)
        {
            return Json(new KeydbService().GetCount(keydbData));
        }

        /// <summary>
        /// 删除项
        /// </summary>
        /// <param name="keydbData"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Del(KeydbData keydbData)
        {
            return Json(new KeydbService().Del(keydbData));
        }

        /// <summary>
        /// 获取string
        /// </summary>
        /// <param name="keydbData"></param>
        /// <returns></returns>
        public ActionResult Get(KeydbData keydbData)
        {
            return Json(new KeydbService().Get(keydbData));
        }

        /// <summary>
        /// 获取子项
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="keydbData"></param>
        /// <returns></returns>
        public ActionResult GetItems(int offset, KeydbData keydbData)
        {
            return Json(new KeydbService().GetItems(offset, keydbData));
        }

        /// <summary>
        /// 修改名称
        /// </summary>
        /// <param name="keydbData"></param>
        /// <param name="newID"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Rename(KeydbData keydbData, string newID)
        {
            return Json(new KeydbService().Rename(keydbData, newID));
        }
        /// <summary>
        /// 修改数据项
        /// </summary>
        /// <param name="keydbData"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(KeydbData keydbData)
        {
            return Json(new KeydbService().Edit(keydbData));
        }

        /// <summary>
        /// 移除项
        /// </summary>
        /// <param name="keydbData"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DelItem(KeydbData keydbData)
        {
            return Json(new KeydbService().DelItem(keydbData));
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd1"></param>
        /// <param name="pwd2"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AlterPWD(string name, string pwd1, string pwd2)
        {
            return Json(new KeydbService().AlterPWD(name, pwd1, pwd2));
        }
    }
}
